package com.shanghaionstar.carmanage;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.ovd.ManagedContentDetailDTO;
import com.onstar.cn.mag.ovd.OVDStatusInfo;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.carmanage.adapter.VechileReportAdapter;
import com.shanghaionstar.carmanage.iview.IVechileReportView;
import com.shanghaionstar.carmanage.model.VechileReportItem;
import com.shanghaionstar.carmanage.presenter.VechileReportPresenter;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.weight.CommonNoDatasLayout;
import com.shanghaionstar.weight.TopLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mengzhou Gu on 2016/7/28.
 */
@ContentView(R.layout.vechile_test_report_activity)
public class VechileTestReportActivity extends SwipeActivity implements IVechileReportView{
    @FindViewById
    private TopLayout           top_layout;
    @FindViewById
    private ProgressLayout      progress_layout;
    @FindViewById
    private CommonNoDatasLayout no_datas_layout;
    @FindViewById
    private TextView            ovd_result_tv_time_id;
    @FindViewById
    private ExpandableListView  ovd_list;
    @FindViewById
    private LinearLayout        datas_layout;
    @FindViewById
    private TextView vechile_report_time;
    private VechileReportPresenter presenter;

    @Override
    public BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        presenter =new VechileReportPresenter(this.getScheduler());
        presenter.setView(this);
        presenter.getOVDEmail();
    }

    @Override
    public void setLoading(boolean isLoading) {
        progress_layout.setVisibility(isLoading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        if (e != null) {
            no_datas_layout.setHintText(R.string.no_ovd_list_data_error_e3120);
        } else {
            no_datas_layout.setHintText(R.string.no_ovd_list_data_error);
        }
        datas_layout.setVisibility(View.GONE);
        no_datas_layout.setVisibility(View.VISIBLE);
    }

    @Override
    public void onReponseSuccess(ManagedContentDetailDTO object) {
        if (object != null) {
            vechile_report_time.setText("("+presenter.getOvdEmailTitle()+")");
            ovd_result_tv_time_id.setText(R.string.option_state_information);
         //   View headView = LayoutInflater.from(onActivity()).inflate(R.layout.discover_ovd_vehicle_condition_header, null);
            List<VechileReportItem> ovdList = convertOVD(object);
            if (ovdList.size() >= 1) {
          //      ovd_list.addHeaderView(headView);
                ovd_list.setAdapter(new VechileReportAdapter(VechileTestReportActivity.this,ovdList));
                ovd_list.setGroupIndicator(null);
            }
        }else{
            no_datas_layout.setHintText(R.string.no_ovd_list_data_error);
            datas_layout.setVisibility(View.GONE);
            no_datas_layout.setVisibility(View.VISIBLE);
        }
    }

    private List<VechileReportItem> convertOVD(ManagedContentDetailDTO dto) {
        if (dto != null && dto.getOvdEmailDTO() != null){
            List<VechileReportItem> list = new ArrayList<VechileReportItem>();
            OVDStatusInfo ovdStatusInfo = dto.getOvdEmailDTO().getOvdStatusInfo();
            if (ovdStatusInfo == null) {
                return list;
            }
            VechileReportItem item=null;
            boolean isVolt = ApplicationUtil.getInstance().isVolt();
            //发动机系统
            if (!CommonUtil.isNull(ovdStatusInfo.getEngineTransmission())&&ovdStatusInfo.getDrainage().getStatus()!=-1){
                item=new VechileReportItem();
                item.icon=ovdStatusInfo.getEngineTransmission().getStatus();
                if (isVolt) {
                    item.title=onActivity().getString(R.string.volt_engineTransmission);
                } else {
                    item.title=onActivity().getString(R.string.engineTransmission);
                }
                if (ovdStatusInfo.getEngineTransmission().getDescription().equals("null")) {
                    item.content=null;
                } else {
                    item.content=ovdStatusInfo.getEngineTransmission().getDescription();
                }
                list.add(item);
            }

            //排放系统
            if (!CommonUtil.isNull(ovdStatusInfo.getDrainage())&&ovdStatusInfo.getDrainage().getStatus()!=-1){
                item=new VechileReportItem();
                item.icon=ovdStatusInfo.getDrainage().getStatus();
                if (isVolt) {
                    item.title=onActivity().getString(R.string.volt_drainage);
                } else {
                    item.title=onActivity().getString(R.string.drainage);
                }
                if (ovdStatusInfo.getDrainage().getDescription().equals("null")) {
                    item.content=null;
                } else {
                    item.content=ovdStatusInfo.getDrainage().getDescription();
                }
                list.add(item);
            }

            //安全气囊系统
            if (!CommonUtil.isNull(ovdStatusInfo.getAirBag())&&ovdStatusInfo.getAirBag().getStatus()!=-1) {
                item=new VechileReportItem();
                item.icon=ovdStatusInfo.getAirBag().getStatus();
                if (isVolt) {
                    item.title=onActivity().getString(R.string.volt_airBag);
                } else {
                    item.title=onActivity().getString(R.string.airBag);
                }
                // 如果显示的内容为空，就不显示内容
                if (!CommonUtil.isNull(ovdStatusInfo.getAirBag())
                        && !CommonUtil.isNull(ovdStatusInfo.getAirBag().getDescription())
                        && !ovdStatusInfo.getAirBag().getDescription().equals("null")) {
                    item.content=ovdStatusInfo.getAirBag().getDescription();
                } else {
                    item.content=null;
                }
                list.add(item);
            }

            //车身稳定控制系统
            if (!CommonUtil.isNull(ovdStatusInfo.getStabiliTrak())&&ovdStatusInfo.getStabiliTrak().getStatus()!=-1) {
                item = new VechileReportItem();
                item.icon=ovdStatusInfo.getStabiliTrak().getStatus();
                if (isVolt) {
                    item.title=onActivity().getString(R.string.volt_stabiliTrak);
                } else {
                    item.title=onActivity().getString(R.string.stabiliTrak);
                }
                // 如果显示的内容为空，就不显示内容
                if (ovdStatusInfo.getStabiliTrak().getDescription().equals("null")) {
                    item.content=null;
                } else {
                    item.content=ovdStatusInfo.getStabiliTrak().getDescription();
                }
                list.add(item);
            }

            //制动防抱死系统
            if (ovdStatusInfo.getAbs() != null&&ovdStatusInfo.getStabiliTrak().getStatus()!=-1) {

                item = new VechileReportItem();
                item.icon=ovdStatusInfo.getAbs().getStatus();
                if (isVolt) {
                    item.title=onActivity().getString(R.string.volt_abs);
                } else {
                    item.title=onActivity().getString(R.string.abs);
                }
                // 如果显示的内容为空，就不显示内容
                if (ovdStatusInfo.getAbs().getDescription().equals("null")) {
                    item.content=null;
                } else {
                    item.content=ovdStatusInfo.getAbs().getDescription();
                }
                list.add(item);
            }

            //系统
            if (!CommonUtil.isNull(ovdStatusInfo.getLithiumBattery())&&ovdStatusInfo.getLithiumBattery().getStatus()!=-1) {

                item = new VechileReportItem();
                item.icon=ovdStatusInfo.getLithiumBattery().getStatus();
                item.title=onActivity().getString(R.string.volt_lithiumBattery);
                // 如果显示的内容为空，就不显示内容
                if (ovdStatusInfo.getLithiumBattery().getDescription().equals("null")) {
                    item.content=null;
                } else {
                    item.content=ovdStatusInfo.getLithiumBattery().getDescription();
                }
                list.add(item);
            }

            //安吉星系统
            if (!CommonUtil.isNull(ovdStatusInfo.getOnStar())&&ovdStatusInfo.getStabiliTrak().getStatus()!=-1) {

                item = new VechileReportItem();
                item.icon=ovdStatusInfo.getOnStar().getStatus();
                if (isVolt) {
                    item.title=onActivity().getString(R.string.volt_onStar);
                } else {
                    item.title=onActivity().getString(R.string.onStar);
                }
                // 如果显示的内容为空，就不显示内容
                if (ovdStatusInfo.getOnStar().getDescription().equals("null")) {
                    item.content=null;
                } else {
                    item.content=ovdStatusInfo.getOnStar().getDescription();
                }
                list.add(item);
            }

            //可发电制动系统
            if (!CommonUtil.isNull(ovdStatusInfo.getElectricDrive())&&ovdStatusInfo.getElectricDrive().getStatus()!=-1) {

                item = new VechileReportItem();
                item.icon=ovdStatusInfo.getElectricDrive().getStatus();
                item.title=onActivity().getString(R.string.volt_electricDrive);
                // 如果显示的内容为空，就不显示内容
                if (ovdStatusInfo.getElectricDrive().getDescription().equals("null")) {
                    item.content=null;
                } else {
                    item.content=ovdStatusInfo.getElectricDrive().getDescription();
                }
                list.add(item);
            }

            return list;
        }else{
            return new ArrayList<VechileReportItem>();
        }

    }

    @Override
    public Activity onActivity() {
        return this;
    }
}
